/*package com.smarttest.portal.controller;

import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinaway.gps.model.GpsCard;
import com.smarttest.common.beans.PaginationResult;
import com.smarttest.portal.pojo.ColdChain;
import com.smarttest.portal.service.ColdChainService;
import com.smarttest.portal.service.ExportFileService;
import com.smarttest.portal.service.GpsCardService;

@Controller
public class ColdChainController {

	private static final Logger log = Logger
			.getLogger(ColdChainController.class);
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	ColdChainService dss;
	@Autowired
	GpsCardService gcs;
	@Autowired
	private ExportFileService exportFileService;

	@RequestMapping(value = "/ColdChain/getData.action", method = RequestMethod.GET)
	@ResponseBody
	public PaginationResult getData(HttpServletRequest request,
			HttpServletResponse response) {
		PaginationResult result = new PaginationResult();

		try {
			String startStr = request.getParameter("start").trim();
			String endStr = request.getParameter("end").trim();
			String reversed = request.getParameter("reversed");
			String pageSizeStr = request.getParameter("pageSize");
			String toPageStr = request.getParameter("toPage");
			String data = request.getParameter("data").trim();
			String dataType = request.getParameter("dataType");
			
			GpsCard card = gcs.getGpsCard(data, dataType);
			Date start = getDateFromString(startStr);
			Date end = getDateFromString(endStr);

			if (card != null && start != null && end != null) {
				int page = Integer.parseInt(toPageStr);
				int rows = Integer.parseInt(pageSizeStr);
				String orderBy = "desc";
				
				if (reversed.equalsIgnoreCase("false")){
					orderBy = "asc";
				}
				result = dss.getPagination(card.getGpsid(), start, end,
						orderBy, page, rows);
			} else {
				log.info("查询失败，数据输入错误 ,data：" + data + " dataType : " + dataType
						+ " startTime : " + startStr + " endTime : " + endStr);
			}

		} catch (Exception e) {
			log.info(e);
		}
		return result;
	}
	
	@RequestMapping(value = "/ColdChain/exportMessage.action", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void exportMessage(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			String startStr = request.getParameter("start").trim();
			String endStr = request.getParameter("end").trim();
			String reversed = request.getParameter("reversed");
			String data = request.getParameter("data").trim();
			String dataType = request.getParameter("dataType");
			startStr = timeLimit(startStr, endStr, 3);
			
			String orderBy = "desc";
			
			if (reversed.equalsIgnoreCase("false")){
				orderBy = "asc";
			}
			
			export(startStr, endStr, orderBy, data, dataType,
					response);
			
		} catch (Exception e){
			log.info(e);
		}
	}

	private void export(String startStr, String endStr, String orderBy,
			String data, String dataType, HttpServletResponse response) throws IOException {
		exportFileService.init(response);
		String[] titleSet = getTitleSet();
		exportFileService.writeTitle(titleSet, response);
		GpsCard card = gcs.getGpsCard(data, dataType);
		Date start = getDateFromString(startStr);
		Date end = getDateFromString(endStr);
		
		if (card != null && start != null && end != null){
			log.info("----exportMessage---------start...");
			Integer pageSize = 1000;
			Integer toPage = 1;
			int count = 0;
			JSONArray jarr = null;
			
			do {
				jarr = getDataArr(card.getGpsid(),start,end,orderBy,toPage,pageSize);
				if (jarr.size() == 0) {
					break;
				}

				int currentCount = jarr.size();
				exportFileService.write(titleSet, jarr, response);

				count += currentCount;
				toPage++;
			} while (jarr != null && jarr.size() == pageSize);
			exportFileService.close(response);
			log.info("-------exportMessage-------end.count:" + count);
		} else {
			log.info("导出失败，数据输入错误 ,data：" + data + " dataType : " + dataType
					+ " startTime : " + startStr + " endTime : " + endStr);
		}
	}

	private JSONArray getDataArr(String imei, Date start, Date end,
			String orderBy, Integer toPage, Integer pageSize) {
		JSONArray jarr = new JSONArray();
		PaginationResult result = dss.getPagination(imei, start, end,
				orderBy, toPage, pageSize);
		jarr.addAll(result.getRows());
		jarr = formatDate(jarr);
		return jarr;
	}

	private JSONArray formatDate(JSONArray jarr) {
		JSONArray formatArr = new JSONArray();
		String[] times = {"starttime","endtime","createtime","updatetime"};
		for(int j = 0; j < jarr.size(); j++){
			JSONObject json = jarr.getJSONObject(j);
			for (int i = 0; i < times.length; i++){
				if (json.containsKey(times[i])){
					String key = times[i];
					Date t = json.getDate(key);
					if (t != null){
						json.remove(key);
						String timestr = sdf.format(t);
						json.put(key, timestr);
					}
				}
			}
			formatArr.add(json);
		}
		return formatArr;
	}

	private String[] getTitleSet() {
		Field[] fields = ColdChain.class.getDeclaredFields();
		String[] titleSet = new String[fields.length];
		for(int i = 0; i < fields.length; i++){
			titleSet[i] = fields[i].getName();
		}
		return titleSet;
	}

	private Date getDateFromString(String s) {
		Date d = null;

		try {
			d = sdf.parse(s);
		} catch (ParseException e) {
			log.info(e);
			d = null;
		}
		return d;
	}
	
	private String timeLimit(String from, String to, int day)
			throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = sdf.parse(from);
		Date end = sdf.parse(to);
		if (end.getTime() - start.getTime() > day * 24 * 60 * 60 * 1000) {
			Calendar cal = Calendar.getInstance();
			// cal.clear();
			cal.setTime(end);
			cal.add(Calendar.DATE, -day);
			start = cal.getTime();
			log.info("导出时间超过" + day + "天，已自动修改开始时间:" + from + "=>"
					+ sdf.format(start));
		}
		return sdf.format(start);
	}

}
*/